package com.ruibo.modules.quartz.controller;

import com.ruibo.modules.exception.BadRequestException;
import com.ruibo.modules.quartz.model.QuartzJob;
import com.ruibo.modules.quartz.service.QuartzJobService;
import com.ruibo.modules.quartz.service.dto.JobQueryCriteria;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/10/16 15:00
 */
@Slf4j
@RestController
@RequestMapping("/jobs")
public class QuartzJobController {
    private static final String ENTITY_NAME = "quartzJob";

    private final QuartzJobService quartzJobService;

    public QuartzJobController(QuartzJobService quartzJobService) {
        this.quartzJobService = quartzJobService;
    }

    /**
     * 获取定时任务列表
     *
     * @param criteria
     * @param pageable
     * @return
     */
    @GetMapping
    public ResponseEntity<Object> getJobs(JobQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(quartzJobService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    /**
     * 新增定时任务
     *
     * @param resources
     * @return
     */
    @PostMapping
    public ResponseEntity<Object> create(@Validated @RequestBody QuartzJob resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return new ResponseEntity<>(quartzJobService.create(resources), HttpStatus.CREATED);
    }

    /**
     * 根据id获取任务信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    //@PreAuthorize("@el.check('timing:edit')")
    public ResponseEntity<Object> findById(@PathVariable Long id) {
        return new ResponseEntity<>(quartzJobService.findById(id), HttpStatus.OK);
    }

    /**
     * 修改定时任务
     *
     * @param resources
     * @return
     */
    @PutMapping
    //@PreAuthorize("@el.check('timing:edit')")
    public ResponseEntity<Object> update(@RequestBody QuartzJob resources) {
        quartzJobService.update(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /**
     * 更改定时任务状态
     *
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public ResponseEntity<Object> updateIsPause(@PathVariable Long id) {
        quartzJobService.updateIsPause(quartzJobService.findById(id));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 执行定时任务
     *
     * @param id
     * @return
     */
    @PutMapping(value = "/exec/{id}")
    public ResponseEntity<Object> execution(@PathVariable Long id) {
        quartzJobService.execution(quartzJobService.findById(id));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除定时任务
     *
     * @param jobIds
     * @return
     */
    @DeleteMapping(value = "/{jobIds}")
    public ResponseEntity<Object> delete(@PathVariable Long[] jobIds) {
        quartzJobService.delete(jobIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
